package com.newspublish.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.newspublish.system.entity.system.Category;
import com.newspublish.system.mapper.CategoryMapper;
import com.newspublish.system.service.CategoryService;
import com.newspublish.system.utils.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result addCategory(Category category) {
        int insert=categoryMapper.insert(category);
        if(insert>0){
            return Result.ok().message("添加分类成功");
        }
        return Result.fail().message("填加分类失败");
    }

    @Override
    public Result deleteCategory(Long id) {
        int delete=categoryMapper.deleteById(id);
        if(delete>0){
            return Result.ok().message("删除分类成功");
        }
        return Result.fail().message("删除分类失败");
    }

    @Override
    public Result updateCategory(Category category) {
        int update=categoryMapper.updateById(category);
        if (update>0) {
            return Result.ok(category).message("修改分类成功");
        }
        return Result.fail().message("修改分类失败");
    }

    @Override
    public Result getAllCategory() {
        List<Category> categories=new ArrayList<>();
        categories=categoryMapper.selectList(null);
        return Result.ok(categories).message("获取所有分类成功");
    }

    @Override
    public Result getByPrimaryKey(Long id) {
        Category category=categoryMapper.selectById(id);
        if(category!=null){
            return Result.ok(category).message("获取分类信息成功");
        }else{
            return Result.fail().message("获取分类信息失败");
        }
    }
}
